package com.infore.dataReceiving.quartz;
import java.util.HashMap;
import java.util.Map;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.infore.dataReceiving.entity.air212.OriginalData;
import com.infore.dataReceiving.repository.air212.IOriginalDataRepository;
import com.infore.dataReceiving.util.Air212InstructionsHandleUtil;
import com.infore.dataReceiving.util.RedisUtil;
/**
 * 实现Job接口
 * @author zzh
 *
 */
@DisallowConcurrentExecution
@Component
public class SchedulerQuartzJob implements Job{
	
	private static final Logger logger = LoggerFactory.getLogger(SchedulerQuartzJob.class);
	
	@Autowired
    private RedisUtil redisUtil;
	
	@Autowired
	private IOriginalDataRepository iOriginalDataRepository;
	
	private static Map<String, String> backstationMap = new HashMap<String, String>();
    
    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
    	String mn = arg0.getJobDetail().getKey().getName();
//    	System.out.println(mn);
    	GetAirBackData(mn);
    }
    
    
	public void GetAirBackData(String mn){

		if(redisUtil==null) {
			System.out.println("redis为空，请联系管理员！");
			logger.error("redis为空，请联系管理员！");
			return;
		}
		while(redisUtil.lGetListSize(mn+"backData")>0) {
			OriginalData data = (OriginalData)redisUtil.leftPop(mn+"backData");
			String msg = "mn="+data.getMn()+"factorCode="+data.getProjectCode()+"dateTime="+data.getReceiveTime();
			if(backstationMap.get(msg)==null) {
				backstationMap.put(msg, "1");
			}else {
				System.out.println("丢弃重复补遗数据");
				continue;
			}
			System.out.println("获取到补遗数据MN："+data.getMn());
			try {
				iOriginalDataRepository.saveAndFlush(data);
			} catch (Exception e) {
				System.out.println("重复补遗数据："+msg);
				logger.error("重复补遗数据："+msg);
			}
		}
		backstationMap = new HashMap<String, String>();
//		Air212InstructionsHandleUtil.dateMap.remove(mn+"backControl");
	}

}
